<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病症" prop="diseaseId">
        <el-select v-model="queryParams.diseaseId" placeholder="请选择" clearable>
          <el-option
            clearable
            v-for="item in diseaseList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="医生" prop="staffId">
        <el-select v-model="queryParams.staffId" placeholder="请选择" clearable>
          <el-option
            clearable
            v-for="item in staffList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="约定日期">
        <el-date-picker
          v-model="daterangeAppointment"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="预约状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择预约状态" clearable>
          <el-option
            v-for="dict in dict.type.reservation_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['schedule:schedule:add']"
        >创建预约</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['schedule:schedule:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['schedule:schedule:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['schedule:schedule:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="病症" align="center" prop="diseaseName" />
      <el-table-column label="症状描述" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="医生" align="center" prop="staffName" />
      <el-table-column label="约定日期" align="center" prop="appointment" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointment, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reservation_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['schedule:schedule:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['schedule:schedule:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户id" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户id" />-->
<!--        </el-form-item>-->
        <el-form-item label="病症" prop="diseaseId">
          <el-select v-model="form.diseaseId" placeholder="请选择" clearable>
            <el-option
              clearable
              v-for="item in diseaseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="医生" prop="staffId">
          <el-select v-model="form.staffId" placeholder="请选择" clearable>
            <el-option
              clearable
              v-for="item in staffList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="症状描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="约定日期" prop="appointment">
          <el-date-picker clearable
            v-model="form.appointment"
            type="date"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            placeholder="请选择约定日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <!-- 提示信息 -->
      <hr>
      <div class="tip-message">
        <p>备注提醒:</p>
        <p>1.本中医馆郑重承诺，将对以上信息保密。</p>
        <p>2.提交成功后，医助会在24小时内给您回电，确认预约信息。</p>
        <p>3.提交成功，不代表已经成功预约，一切以医助回电通知为准。</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule } from "@/api/schedule/schedule";
import { listDisease, getDisease, delDisease, addDisease, updateDisease } from "@/api/disease/disease";
import { listStaff, getStaff, delStaff, addStaff, updateStaff } from "@/api/staff/staff";

export default {
  name: "Schedule",
  dicts: ['reservation_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的预约表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预约状态时间范围
      daterangeAppointment: [],
      // 只能选择今天及以后的日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        diseaseId: null,
        staffId: null,
        name: null,
        phone: null,
        appointment: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        diseaseId: [
          { required: true, message: "请选择病症", trigger: "blur" }
        ],
        staffId: [
          { required: true, message: "请选择医生", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话号码不能为空", trigger: "blur" }
        ],
        appointment: [
          { required: true, message: "请选择到诊日期", trigger: "blur" }
        ],
      },
      // 病症列表
      diseaseList: [],
      // 医生列表
      staffList: []
    };
  },
  created() {
    this.getList();
    this.getStaffList();
    this.getDiseaseList();
  },
  methods: {
    /** 查询我的预约列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAppointment && '' != this.daterangeAppointment) {
        this.queryParams.params["beginAppointment"] = this.daterangeAppointment[0];
        this.queryParams.params["endAppointment"] = this.daterangeAppointment[1];
      }
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询员工信息列表 */
    getStaffList() {
      this.loading = true;
      listStaff({
        pageNum: 1,
        pageSize: 100
      }).then(response => {
        this.staffList = response.rows;
        this.loading = false;
      });
    },
    /** 查询病症信息列表 */
    getDiseaseList() {
      this.loading = true;
      listDisease({
        pageNum: 1,
        pageSize: 100,
        status: 1,
      }).then(response => {
        this.diseaseList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        diseaseId: null,
        staffId: null,
        name: null,
        phone: null,
        description: null,
        appointment: null,
        status: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAppointment = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的预约";
      // 用户新增状态直接为 已预约 10
      this.form.status = 10;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchedule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除我的预约编号为"' + ids + '"的数据项？').then(function() {
        return delSchedule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('schedule/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.tip-message p {
  margin: 1px 0; /* 调整段落上下间距 */
}

.tip-message p:first-of-type {
  color: red; /* 将第一个段落的文字设置为红色 */
  font-weight: bold; /* 将第一个段落的文字设置为加粗 */
}

</style>
